import { getMasterPlanByPage, addMasterPlan, updateMasterPlan, delMasterPlan, exportMasterPlan } from '@/services/project/masterPlan';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Tree } from 'antd';
import { sortTran, filterTran } from '@/utils/requestUtil';
import React, { useEffect, useRef, useState } from 'react';
const MasterPlan: React.FC<{}> = () => {
const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const [createOrUpdate, setCreateOrUpdate] = useState(false); //编辑框的disable
  const [form] = Form.useForm();
    const ref = useRef(null);
  const columns: ProColumns[] = [
        {
        title: '序号',
        dataIndex: 'index',
        valueType: 'index',
        width: 80,
        },
        {
          title: '调试结束',
          dataIndex: 'commissionEndDate',
        },
        {
          title: '调试周期',
          dataIndex: 'commissionPeriod',
        },
        {
          title: '调试开始',
          dataIndex: 'commissionStartDate',
        },
        {
          title: '交货日期',
          dataIndex: 'deliveryDate',
        },
        {
          title: '交货结束',
          dataIndex: 'deliveryEndDate',
        },
        {
          title: '交货周期',
          dataIndex: 'deliveryPeriod',
        },
        {
          title: '交货开始',
          dataIndex: 'deliveryStartDate',
        },
        {
          title: '设计结束',
          dataIndex: 'designEndDate',
        },
        {
          title: '设计周期',
          dataIndex: 'designPeriod',
        },
        {
          title: '设计开始',
          dataIndex: 'designStartDate',
        },
        {
          title: '入库结束',
          dataIndex: 'inboundEndDate',
        },
        {
          title: '入库周期',
          dataIndex: 'inboundPeriod',
        },
        {
          title: '入库开始',
          dataIndex: 'inboundStartDate',
        },
        {
          title: '安装结束',
          dataIndex: 'installEndDate',
        },
        {
          title: '安装周期',
          dataIndex: 'installPeriod',
        },
        {
          title: '安装开始',
          dataIndex: 'installStartDate',
        },
        {
          title: '开工结束',
          dataIndex: 'kickoffEndDate',
        },
        {
          title: '开工周期',
          dataIndex: 'kickoffPeriod',
        },
        {
          title: '开工开始',
          dataIndex: 'kickoffStartDate',
        },
        {
          title: '请料结束',
          dataIndex: 'logistcEndDate',
        },
        {
          title: '请料周期',
          dataIndex: 'logistcPeriod',
        },
        {
          title: '请料开始',
          dataIndex: 'logistcStartDate',
        },
        {
          title: '计划编码',
          dataIndex: 'masterPlanCode',
        },
        {
          title: '备注',
          dataIndex: 'memo',
        },
        {
          title: '生产结束',
          dataIndex: 'orderEndDate',
        },
        {
          title: '生产周期',
          dataIndex: 'orderPeriod',
        },
        {
          title: '生产开始',
          dataIndex: 'orderStartDate',
        },
        {
          title: '打包结束',
          dataIndex: 'packEndDate',
        },
        {
          title: '打包周期',
          dataIndex: 'packPeriod',
        },
        {
          title: '打包开始',
          dataIndex: 'packStartDate',
        },
        {
          title: '采购结束',
          dataIndex: 'purchaseEndDate',
        },
        {
          title: '采购周期',
          dataIndex: 'purchasePeriod',
        },
        {
          title: '采购开始',
          dataIndex: 'purchaseStartDate',
        },
        {
          title: '签收结束',
          dataIndex: 'receiptEndDate',
        },
        {
          title: '签收周期',
          dataIndex: 'receiptPeriod',
        },
        {
          title: '签收开始',
          dataIndex: 'receiptStartDate',
        },
        {
          title: '预留',
          dataIndex: 'reserved',
        },
        {
          title: '流水号',
          dataIndex: 'serialNo',
        },
        {
          title: '项目id',
          dataIndex: 'infoId',
        },
        {
          title: '验收结束',
          dataIndex: 'facEndDate',
        },
        {
          title: '验收周期',
          dataIndex: 'facPeriod',
        },
        {
          title: '验收开始',
          dataIndex: 'facStartDate',
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
        },
        {
          title: '验收结束',
          dataIndex: 'facendDate',
        },
        {
          title: '验收周期',
          dataIndex: 'facperiod',
        },
        {
          title: '验收开始',
          dataIndex: 'facstartDate',
        },
        {
        title: '创建日期', //表头显示的名称
        search: false,
        dataIndex: 'createTime',
        },
        {
        title: '创建人', //表头显示的名称
        search: false,
        dataIndex: 'createName',
        },
        {
        title: '操作',
        valueType: 'option',
        key: 'option',
        fixed: 'right',
        render: (text, record, _, action) => [
        <a
                key="editKey"
                onClick={() => {

        setModalVisit(true);
        setCreateOrUpdate(true);
        setFormTitle('编辑');
        form.setFieldsValue({
        commissionEndDate: record.commissionEndDate,
        commissionPeriod: record.commissionPeriod,
        commissionStartDate: record.commissionStartDate,
        deliveryDate: record.deliveryDate,
        deliveryEndDate: record.deliveryEndDate,
        deliveryPeriod: record.deliveryPeriod,
        deliveryStartDate: record.deliveryStartDate,
        designEndDate: record.designEndDate,
        designPeriod: record.designPeriod,
        designStartDate: record.designStartDate,
        inboundEndDate: record.inboundEndDate,
        inboundPeriod: record.inboundPeriod,
        inboundStartDate: record.inboundStartDate,
        installEndDate: record.installEndDate,
        installPeriod: record.installPeriod,
        installStartDate: record.installStartDate,
        kickoffEndDate: record.kickoffEndDate,
        kickoffPeriod: record.kickoffPeriod,
        kickoffStartDate: record.kickoffStartDate,
        logistcEndDate: record.logistcEndDate,
        logistcPeriod: record.logistcPeriod,
        logistcStartDate: record.logistcStartDate,
        masterPlanCode: record.masterPlanCode,
        memo: record.memo,
        orderEndDate: record.orderEndDate,
        orderPeriod: record.orderPeriod,
        orderStartDate: record.orderStartDate,
        packEndDate: record.packEndDate,
        packPeriod: record.packPeriod,
        packStartDate: record.packStartDate,
        purchaseEndDate: record.purchaseEndDate,
        purchasePeriod: record.purchasePeriod,
        purchaseStartDate: record.purchaseStartDate,
        receiptEndDate: record.receiptEndDate,
        receiptPeriod: record.receiptPeriod,
        receiptStartDate: record.receiptStartDate,
        reserved: record.reserved,
        serialNo: record.serialNo,
        infoId: record.infoId,
        facEndDate: record.facEndDate,
        facPeriod: record.facPeriod,
        facStartDate: record.facStartDate,
        projectName: record.projectName,
        facendDate: record.facendDate,
        facperiod: record.facperiod,
        facstartDate: record.facstartDate,
        id: record.id,
        });
        }}
        >
        编辑
        </a>,
        <a
                key="deleteKey"
                onClick={() => {


        Modal.confirm({
        title: '删除',
        content: '确定要删除吗',
        okText: '确认',
        cancelText: '取消',
        onOk: async () => {
        await delMasterPlan({ id: record.id });
        actionRef.current.reload();
        },
        });

        }}
        >
        删除
        </a>,
        ],
        },
  ];
  const handleSubmit = async () => {
        setConfirmLoading(true);
        if (formTitle == '新建') {
          const result = await addMasterPlan(form.getFieldsValue());
        if (result != null) {
          message.success('添加成功');
        }
        } else {
          await updateMasterPlan(form.getFieldsValue());
        }
        setConfirmLoading(false);
        setModalVisit(false);
        actionRef.current.reload();
  };

  return (
  <PageHeaderWrapper>
    <ProTable
        headerTitle="xx查询"
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        formRef={ref}
        toolBarRender={(action, { selectedRows }) => [
              <Button
                key="button"
                icon={<PlusOutlined />}
                onClick={() => {
                    setCreateOrUpdate(false);
                    setFormTitle('新建');
                    form.resetFields();
                    setModalVisit(true);
                }}
                type="primary"
                >
                新建
                </Button>,
                <Button
                        key="button"
                        icon={<PlusOutlined />}
                onClick={() => {
                const filters = filterTran(ref.current.getFieldsValue());
                exportMasterPlan({
                    current: 1,
                    pageSize: 5000,
                    filters: filters,
                }).then((res) => {
                const aLink = document.createElement('a');
                document.body.appendChild(aLink);
                aLink.style.display = 'none';
                aLink.href = window.URL.createObjectURL(res);
                aLink.setAttribute('download', `MasterPlan.xlsx`);
                aLink.click();
                document.body.removeChild(aLink);
                });
                }}
                type="primary"
                >
                导出
                </Button>,
        ]}
        request={async (params = {}, sort, filter) => {

                const sorters = sortTran(sort);
                const filters = filterTran(params);
                const result = await getMasterPlanByPage({ ...params, sorters: sorters, filters: filters });
                return {
                data: result.data.content,
                total: result.data.totalElements,
                success: result.success,
                };

        }}
        columns={columns}
        scroll={{ x: 2000, y: 350 }}
        rowSelection={{}}
    />
    <Modal
            title={formTitle}
            visible={modalVisit}
            onOk={handleSubmit}
            confirmLoading={confirmLoading}
            onCancel={() => setModalVisit(false)}
    >
    <Form
            name="wrap"
            labelCol={{ flex: '110px' }}
            labelAlign="left"
            labelWrap
            form={form}
            wrapperCol={{ flex: 1 }}
            colon={false}
    >
          <Form.Item label="id" name="id" hidden={true}>
            <Input />
          </Form.Item>
          <Form.Item
                  label="调试结束"
                  name="commissionEndDate"
                  rules={[{ required: true, message: 'Please input your commissionEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="调试周期"
                  name="commissionPeriod"
                  rules={[{ required: true, message: 'Please input your commissionPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="调试开始"
                  name="commissionStartDate"
                  rules={[{ required: true, message: 'Please input your commissionStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="交货日期"
                  name="deliveryDate"
                  rules={[{ required: true, message: 'Please input your deliveryDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="交货结束"
                  name="deliveryEndDate"
                  rules={[{ required: true, message: 'Please input your deliveryEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="交货周期"
                  name="deliveryPeriod"
                  rules={[{ required: true, message: 'Please input your deliveryPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="交货开始"
                  name="deliveryStartDate"
                  rules={[{ required: true, message: 'Please input your deliveryStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="设计结束"
                  name="designEndDate"
                  rules={[{ required: true, message: 'Please input your designEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="设计周期"
                  name="designPeriod"
                  rules={[{ required: true, message: 'Please input your designPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="设计开始"
                  name="designStartDate"
                  rules={[{ required: true, message: 'Please input your designStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="入库结束"
                  name="inboundEndDate"
                  rules={[{ required: true, message: 'Please input your inboundEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="入库周期"
                  name="inboundPeriod"
                  rules={[{ required: true, message: 'Please input your inboundPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="入库开始"
                  name="inboundStartDate"
                  rules={[{ required: true, message: 'Please input your inboundStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="安装结束"
                  name="installEndDate"
                  rules={[{ required: true, message: 'Please input your installEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="安装周期"
                  name="installPeriod"
                  rules={[{ required: true, message: 'Please input your installPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="安装开始"
                  name="installStartDate"
                  rules={[{ required: true, message: 'Please input your installStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="开工结束"
                  name="kickoffEndDate"
                  rules={[{ required: true, message: 'Please input your kickoffEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="开工周期"
                  name="kickoffPeriod"
                  rules={[{ required: true, message: 'Please input your kickoffPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="开工开始"
                  name="kickoffStartDate"
                  rules={[{ required: true, message: 'Please input your kickoffStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="请料结束"
                  name="logistcEndDate"
                  rules={[{ required: true, message: 'Please input your logistcEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="请料周期"
                  name="logistcPeriod"
                  rules={[{ required: true, message: 'Please input your logistcPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="请料开始"
                  name="logistcStartDate"
                  rules={[{ required: true, message: 'Please input your logistcStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="计划编码"
                  name="masterPlanCode"
                  rules={[{ required: true, message: 'Please input your masterPlanCode!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="备注"
                  name="memo"
                  rules={[{ required: true, message: 'Please input your memo!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="生产结束"
                  name="orderEndDate"
                  rules={[{ required: true, message: 'Please input your orderEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="生产周期"
                  name="orderPeriod"
                  rules={[{ required: true, message: 'Please input your orderPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="生产开始"
                  name="orderStartDate"
                  rules={[{ required: true, message: 'Please input your orderStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="打包结束"
                  name="packEndDate"
                  rules={[{ required: true, message: 'Please input your packEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="打包周期"
                  name="packPeriod"
                  rules={[{ required: true, message: 'Please input your packPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="打包开始"
                  name="packStartDate"
                  rules={[{ required: true, message: 'Please input your packStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="采购结束"
                  name="purchaseEndDate"
                  rules={[{ required: true, message: 'Please input your purchaseEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="采购周期"
                  name="purchasePeriod"
                  rules={[{ required: true, message: 'Please input your purchasePeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="采购开始"
                  name="purchaseStartDate"
                  rules={[{ required: true, message: 'Please input your purchaseStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="签收结束"
                  name="receiptEndDate"
                  rules={[{ required: true, message: 'Please input your receiptEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="签收周期"
                  name="receiptPeriod"
                  rules={[{ required: true, message: 'Please input your receiptPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="签收开始"
                  name="receiptStartDate"
                  rules={[{ required: true, message: 'Please input your receiptStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="预留"
                  name="reserved"
                  rules={[{ required: true, message: 'Please input your reserved!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="流水号"
                  name="serialNo"
                  rules={[{ required: true, message: 'Please input your serialNo!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="项目id"
                  name="infoId"
                  rules={[{ required: true, message: 'Please input your infoId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="验收结束"
                  name="facEndDate"
                  rules={[{ required: true, message: 'Please input your facEndDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="验收周期"
                  name="facPeriod"
                  rules={[{ required: true, message: 'Please input your facPeriod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="验收开始"
                  name="facStartDate"
                  rules={[{ required: true, message: 'Please input your facStartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="项目名称"
                  name="projectName"
                  rules={[{ required: true, message: 'Please input your projectName!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="验收结束"
                  name="facendDate"
                  rules={[{ required: true, message: 'Please input your facendDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="验收周期"
                  name="facperiod"
                  rules={[{ required: true, message: 'Please input your facperiod!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="验收开始"
                  name="facstartDate"
                  rules={[{ required: true, message: 'Please input your facstartDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
        </Form>
      </Modal>
  </PageHeaderWrapper>
  );
  };
  export default MasterPlan;
